package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Goods;
import com.wms.entity.Record;
import com.wms.entity.Request;
import com.wms.service.GoodsService;
import com.wms.service.RecordService;
import com.wms.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/request")
public class RequestController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RequestService requestService;




    @PostMapping("/listPageCCC")
    public  Result listPageCCC(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String storage = (String)param.get("storage");
        String goodstype = (String)param.get("goodstype");
        String userId = (String)param.get("userId");
//        String action = (String)param.get("action");
//        Integer count = (Integer)param.get("count");
//
//        if("2".equals(action)){
//            count = -count;
//            record.setCount(count1);
//
//        }


        System.out.println("name==="+(String)param.get("name"));


        Page<Request> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        QueryWrapper<Request> queryWrapper = new QueryWrapper();
        queryWrapper.apply(" a.goods=b.id AND  b.storage= c.id AND b.goodstype = d.id").last("order by a.id asc");


        if(StringUtils.isNotBlank(name)&&!"null".equals(name)){

            queryWrapper.like("b.name",name);

        }
        if(StringUtils.isNotBlank(storage)&&!"null".equals(storage)){

            queryWrapper.eq("c.id",storage);
        }
        if(StringUtils.isNotBlank(goodstype)&&!"null".equals(goodstype)){

            queryWrapper.eq("d.id",goodstype);

        }


        IPage result = requestService.pageCCC(page,queryWrapper);

        return Result.suc(result.getRecords(), result.getTotal());
    }











    @PostMapping("/save")
    public Result save(@RequestBody Request request){
        return requestService.save(request)?Result.suc():Result.fail();
    }





    @PostMapping("/update")
    public Result update(@RequestBody Request request)
    {


        Goods goods = goodsService.getById(request.getGoods());
        Integer count1 = request.getCount();/*记录中变动物品的数量*/
//        if("2".equals(request.getAction())){
//            count1 = -count1;
//            request.setCount(count1);
//
//        }




        Integer count = goods.getCount();  /* 变动物品前的数量*/
        Integer sum = count  + count1;     /*变动物品后的数量*/
        goods.setCount(sum);
        goodsService.updateById(goods);

//        实现记录增加


        Record record = new Record();
        record.setGoods(request.getGoods());
        record.setUserid(request.getUserid());
        record.setAdminId(request.getAdminId());
        record.setCount(request.getCount());
        record.setRemark(request.getRemark());

        requestService.removeById(request.getId());


        return recordService.save(record)?Result.suc():Result.fail();

    }

    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return requestService.removeById(id)?Result.suc():Result.fail();
    }


}
